package com.imu.purse.modules.trade.task;


import com.imu.purse.modules.bitcoin.YtfcoinApi;
import com.imu.purse.modules.trade.entity.FundEntity;
import com.imu.purse.modules.trade.entity.TransDetailLogEntity;
import com.imu.purse.modules.trade.entity.TransLogEntity;
import com.imu.purse.modules.trade.service.FundService;
import com.imu.purse.modules.trade.service.TransDetailLogService;
import com.imu.purse.modules.trade.service.TransLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component("resolveTransLog")
public class ResolveTransLog {

    private static final Logger log = LoggerFactory.getLogger(ResolveTransLog.class);


    @Autowired
    private TransLogService transLogService;

    @Autowired
    private TransDetailLogService transDetailLogService;

    @Autowired
    private YtfcoinApi ytfcoinApi;

    @Autowired
    private FundService fundService;

    public void resTransLog(){
        log.info("Start resTransLog:" + System.currentTimeMillis());

        //当前页数
        int page = 1;
        //每次最多取1000条
        int limit = 1000;
        //获取待处理的日志

        TransLogEntity transLog = new TransLogEntity();
        transLog.setOrderState(1); //查询
        transLog.setPage(page);
        transLog.setLimit(limit);

        while (true){
            List<TransLogEntity> transLogEntitys = transLogService.queryList(transLog);
            if(CollectionUtils.isEmpty(transLogEntitys)){
                break;
            }

            for(TransLogEntity transLogEntity : transLogEntitys) {
                TransDetailLogEntity transDetailLogEntity = new TransDetailLogEntity();
                transDetailLogEntity.setLogId(transLogEntity.getId());
                List<TransDetailLogEntity> TransDetailLogEntitys =  transDetailLogService.queryList(transDetailLogEntity);
                if(null == TransDetailLogEntitys || TransDetailLogEntitys.size() == 0) {
                }else {
                    transDetailLogEntity = TransDetailLogEntitys.get(0);

                    //更新主表记录
                    transLogEntity.setOrderState(2);
                    transLogEntity.setUpdateTime(new Date());
                    transLogService.save(transLogEntity);

                    //todo 开始转账
                    FundEntity fundEntity = fundService.findByUserId(transLogEntity.getUserId());

                    ytfcoinApi.sendTransaction(transDetailLogEntity.getRedAddr(),
                            transDetailLogEntity.getPayAddr(),
                            transLogEntity.getMoney().toString(),
                            fundEntity.getPassword(),
                            fundEntity.getMoneyAddress(),0);

                    //生成子订单记录
                    TransDetailLogEntity transDetailLogEntity2 = new TransDetailLogEntity();
                    transDetailLogEntity2.setLogId(transDetailLogEntity.getLogId());
                    transDetailLogEntity2.setPayAddr(transDetailLogEntity.getRedAddr());
                    transDetailLogEntity2.setRedAddr(transDetailLogEntity.getPayAddr());
                    transDetailLogEntity2.setMoney(BigDecimal.valueOf(0L));
                    transDetailLogEntity2.setCreateTime(new Date());
                    transDetailLogService.save(transDetailLogEntity2);

                    //更新主表记录到完成
                    transLogEntity.setOrderState(3);
                    transLogEntity.setUpdateTime(new Date());
                    transLogService.save(transLogEntity);
                }


            }
            page++;
            if(transLogEntitys.size() < limit){
                break;
            }

        }

    }


}
